package com.example.wan1.view.collect;

import com.example.wan1.bus.RxBus;
import com.example.wan1.data.BaseObserver;
import com.example.wan1.data.IsNotObserver;
import com.example.wan1.data.RetrofitUtil;
import com.example.wan1.data.RxSchedulers;
import com.example.wan1.event.LoginEvent;
import com.example.wan1.event.NotifyOperateEvent;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.model.CollectWebBean;
import com.example.wan1.utils.ToastUtils;
import com.example.wan1.view.login.UserManager;

/***
 * 创建时间：2020/7/25 18:25
 * 创建人：10850
 * 功能描述：处理收藏和取消收藏的工具类
 */
@SuppressWarnings("unchecked")
public class HandleCollectTool {

    private volatile static HandleCollectTool instance;

    public static HandleCollectTool getInstance() {
        if (instance == null) {
            synchronized (HandleCollectTool.class) {
                if (instance == null) {
                    instance = new HandleCollectTool();
                }
            }
        }
        return instance;
    }

    public ArticleBean getArticle() {
        return mArticle;
    }

    public void setArticle(ArticleBean article) {
        mArticle = article;
    }

    private ArticleBean mArticle;

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    private int position;

    public void setInfo(ArticleBean article, int position, String type) {
        setArticle(article);
        setPosition(position);
        setType(type);
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    private String type;


    public void collectIn() {
        /*RetrofitUtil.getInstance().collectInArticle(mArticle.getId())
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new Observer<CommonResBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommonResBody commonResBody) {
                        int errorCode = commonResBody.getErrorCode();
                        Log.i("zxd", "onNext: " + errorCode);
                        if (errorCode == 0) {
                            ToastUtils.showToast("收藏成功");
                            mArticle.setCollect(true);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });*/

        RetrofitUtil.getInstance().collectInArticle(mArticle.getId())
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        if (code == 0) {
                            mArticle.setCollect(true);
                            ToastUtils.showToast("收藏成功!");
                        } else {
                            ToastUtils.showToast("收藏失败!");
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        //ToastUtils.showToast(errorMsg);
                        if (errorMsg.contains("出现异常")) {
                            //ToastUtils.showToast("请登录!");
                        }
                    }
                });
    }

    public void collectOut(String title, String author, String link) {
        /*RetrofitUtil.getInstance().collectOutArticle(mArticle.getTitle(), mArticle.getAuthor(), mArticle.getLink())
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new Observer<CommonResBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommonResBody commonResBody) {
                        int errorCode = commonResBody.getErrorCode();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });*/

        RetrofitUtil.getInstance().collectOutArticle(title, author, link)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        if (code == 0) {
                            ToastUtils.showToast("收藏成功!");
                        } else {
                            ToastUtils.showToast("收藏失败!");
                        }
                        RxBus.getDefault().post(new NotifyOperateEvent("收藏"));
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        //ToastUtils.showToast(errorMsg);
                    }
                });
    }

    public void cancelList() {
        /*RetrofitUtil.getInstance().cancelCollectArticleList(mArticle.getId())
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new Observer<CommonResBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommonResBody commonResBody) {
                        int errorCode = commonResBody.getErrorCode();
                        if (errorCode == 0) {
                            ToastUtils.showToast("取消收藏成功");
                        }
                        Log.i("zxd", "onNext: " + errorCode);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });*/

        RetrofitUtil.getInstance().cancelCollectArticleList(mArticle.getId())
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        if (code == 0) {
                            mArticle.setCollect(false);
                            ToastUtils.showToast("取消收藏成功!");
                        } else {
                            ToastUtils.showToast("取消收藏失败!");
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(errorMsg);
                    }
                });
    }

    public void cancelMyCollect(int id, int originId) {
        RetrofitUtil.getInstance().cancelMyCollect(id, originId)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        ToastUtils.showToast("取消收藏成功!");
                        RxBus.getDefault().post(new NotifyOperateEvent("收藏"));
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(errorMsg);
                    }
                });
    }

    public void addCollect(String name, String link) {
        /*RetrofitUtil.getInstance().addCollect(name, title)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        if (code == 0) {
                            ToastUtils.showToast("添加收藏网站成功!");
                        } else {
                            ToastUtils.showToast("添加收藏网站失败!");
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(errorMsg);
                    }
                });*/

        RetrofitUtil.getInstance().addCollect(name, link)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<CollectWebBean>() {

                    @Override
                    public void onSuccess(CollectWebBean result) {
                        ToastUtils.showToast("添加收藏网站成功!");
                        RxBus.getDefault().post(new NotifyOperateEvent("网站"));
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(errorMsg);
                    }
                });
    }

    public void editCollect(int id, String name, String link) {
        /*RetrofitUtil.getInstance().editCollect(id, name, title)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        if (code == 0) {
                            ToastUtils.showToast("修改收藏网站成功!");
                        } else {
                            ToastUtils.showToast("修改收藏网站失败!");
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(errorMsg);
                    }
                });*/

        RetrofitUtil.getInstance().editCollect(id, name, link)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<CollectWebBean>() {
                    @Override
                    public void onSuccess(CollectWebBean result) {
                        ToastUtils.showToast("修改收藏网站成功!");
                        RxBus.getDefault().post(new NotifyOperateEvent("网站"));
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(errorMsg);
                    }
                });
    }

    public void delCollect(int id) {
        /*RetrofitUtil.getInstance().delCollect(id)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new IsNotObserver() {
                    @Override
                    public void onSuccess(int code, String result) {
                        if (code == 0) {
                            ToastUtils.showToast("删除收藏网站成功!");
                        } else {
                            ToastUtils.showToast("删除收藏网站失败!");
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(errorMsg);
                    }
                });*/

        RetrofitUtil.getInstance().delCollect(id)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<CollectWebBean>() {
                    @Override
                    public void onSuccess(CollectWebBean result) {
                        ToastUtils.showToast("删除收藏网站成功!");
                        RxBus.getDefault().post(new NotifyOperateEvent("网站"));
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(errorMsg);
                    }
                });
    }

    public void delShare(int id) {
        RetrofitUtil.getInstance().delShare(id)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<ArticleBean>() {
                    @Override
                    public void onSuccess(ArticleBean result) {
                        ToastUtils.showToast("删除分享文章成功!");
                        RxBus.getDefault().post(new NotifyOperateEvent("分享"));
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(errorMsg);
                    }
                });
    }

    public void addShare(String title, String link) {
        RetrofitUtil.getInstance().addShare(title, link)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<ArticleBean>() {
                    @Override
                    public void onSuccess(ArticleBean result) {
                        ToastUtils.showToast("添加分享文章成功!");
                        RxBus.getDefault().post(new NotifyOperateEvent("分享"));
                        UserManager.getInstance().refresh();
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtils.showToast(errorMsg);
                    }
                });
    }
}
